#include <stdio.h>

#define  SIZE 10
#define printNumber(a)   printf("%d  ", (a));

void PrintMatrixClockwisely(int *array, int columns, int rows);
void PrintMatrixInCircle(int *, int, int, int);
int main()
{
    int array[SIZE] = {1,2,3,4,5,6,7,8,9};
    PrintMatrixClockwisely(array, 3, 3);
    return 0;
}

void PrintMatrixClockwisely(int *array, int columns, int rows)
{
    if (NULL == array || columns <= 0 || rows <= 0)
        return;

    int start = 0;

    while(columns > start * 2 && rows > start * 2) {
        PrintMatrixInCircle(array, columns, rows, start);
        start++;
    }
}



void PrintMatrixInCircle(int * array, int columns, int rows, int start)
{
    int endX = columns - 1 -start;
    int endY = rows - 1 - start;
    int i, number;

    //print the row from left to right;
    for (i = start; i <= endX; i++) {
        number = array[start * columns + i];
        printNumber(number);
    }

    //print the column from up to down;
    if (start < endY) {
        for (i = start + 1; i <= endY; i++) {
           number = array[i * columns + endX];
           printNumber(number);
        }
    }

    //print the rows from right to left;
    if (start < endX && start < endY) {
        for (i = endX - 1; i >= start; i--) {
            number = array[endY * columns + i];
            printNumber(number);
        }
    }

    //print the columns from down to up;
    if (start < endX && start < endY - 1) {
        for (i = endY - 1; i >= start + 1; i--) {
            number = array[i * columns + start];
            printNumber(number);
        }

    }
}
